package com.dishdex.menuhub.console.controller.user;

import com.dishdex.menuhub.console.annotations.VerifiedUser;
import com.dishdex.menuhub.console.domain.user.*;
import com.dishdex.menuhub.console.util.UserConverter;
import com.dishdex.menuhub.module.module.user.entity.User;
import com.dishdex.menuhub.module.module.user.service.UserService;
import com.dishdex.menuhub.module.utils.BaseUtils;
import com.dishdex.menuhub.module.utils.Response;
import com.dishdex.menuhub.module.utils.SignUtils;
import com.dishdex.menuhub.module.utils.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:
 * Package: com.dishdex.menuhub.console.controller.user
 * Description:
 *
 * @Author:
 * @Create 2025/10/22 10:00
 * @Version 1.0
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/user/login")
    public Response userLogin(@RequestParam(value = "phone", required = false) String phone,
                              @RequestParam(value = "password", required = false) String password,
                              HttpServletResponse response,
                              HttpSession httpSession,
                              @VerifiedUser User loginUser) {
        if (!BaseUtils.isEmpty(loginUser)) {
            return new Response(4004);
        }
        Boolean result;
        try {
            result = userService.login(phone, password);
        } catch (RuntimeException e) {
            UserLoginInfoVo userErrorVO = new UserLoginInfoVo();
            userErrorVO.setMessage(e.getMessage());
            return new Response(4004, userErrorVO);
        }
        UserLoginInfoVo userLoginInfoVo = new UserLoginInfoVo();
        if (result) {
            userLoginInfoVo.setMessage("登录成功");
            //使用Cookie登录
            User user = userService.getByPhone(phone);
            Cookie cookie = new Cookie(SpringUtils.getProperty("application.cookie.key"), SignUtils.makeSign(user.getId()));
            cookie.setPath("/");
            cookie.setMaxAge(SignUtils.EXPIRATION_TIME);
            //cookie.setDomain();
            response.addCookie(cookie);

            //使用Session登录
            //httpSession.setAttribute(SpringUtils.getProperty("application.session.key"),BaseUtils.makeSign(user.getId()));
        } else {
            return new Response(1010);
        }
        return new Response(1001, userLoginInfoVo);
    }


    @RequestMapping("/user/list")
    public Response userList(@VerifiedUser User loginUser) {
        if (BaseUtils.isEmpty(loginUser)) {
            return new Response(1002);
        }
        List<User> userList = userService.getAll();
        List<UserItemVO> userItemVOList = new ArrayList<>();
        for (User user : userList) {
            userItemVOList.add(UserConverter.toUserItemVO(user));
        }
        UserListVO userListVO = new UserListVO();
        userListVO.setList(userItemVOList);
        return new Response(1001, userListVO);
    }

    @RequestMapping("/user/info")
    public Response userInfo(@RequestParam(value = "userId", required = false) Long userId,
                             @VerifiedUser User loginUser) {
        if (BaseUtils.isEmpty(loginUser)) {
            return new Response(1002);
        }
        User user;
        try {
            user = userService.getById(userId);
        } catch (RuntimeException e) {
            UserDetailVO userErrorVO = new UserDetailVO();
            userErrorVO.setMessage(e.getMessage());
            return new Response(4004, userErrorVO);
        }
        UserDetailVO userDetailVO = new UserDetailVO()
                .setAvatar(user.getAvatar())
                .setPhone(user.getPhone())
                .setPassword(user.getPassword())
                .setName(user.getName())
                .setCreateTime(String.valueOf(user.getCreateTime()))
                .setUpdateTime(String.valueOf(user.getUpdateTime()));
        return new Response(1001, userDetailVO);
    }

    @RequestMapping("/user/insert")
    public Response userInsert(@RequestParam(value = "avatar", required = false) String avatar,
                               @RequestParam(value = "phone", required = false) String phone,
                               @RequestParam(value = "password", required = false) String password,
                               @RequestParam(value = "name", required = false) String name,
                               @VerifiedUser User loginUser) {
        if (BaseUtils.isEmpty(loginUser)) {
            return new Response(1002);
        }
        Long result;
        try {
            result = userService.edit(null, avatar, phone, password, name);
        } catch (RuntimeException e) {
            UserOperationResultVO userErrorVO = new UserOperationResultVO();
            userErrorVO.setMessage(e.getMessage());
            return new Response(4004, userErrorVO);
        }
        UserOperationResultVO userOperationResultVO = new UserOperationResultVO();
        userOperationResultVO.setMessage(String.valueOf(result));
        return new Response(1001, userOperationResultVO);
    }

    @RequestMapping("/user/update")
    public Response userUpdate(@RequestParam(value = "userId", required = false) Long userId,
                               @RequestParam(value = "avatar", required = false) String avatar,
                               @RequestParam(value = "phone", required = false) String phone,
                               @RequestParam(value = "password", required = false) String password,
                               @RequestParam(value = "name", required = false) String name,
                               @VerifiedUser User loginUser) {
        if (BaseUtils.isEmpty(loginUser)) {
            return new Response(1002);
        }
        Long result;
        try {
            result = userService.edit(userId, avatar, phone, password, name);
        } catch (RuntimeException e) {
            UserOperationResultVO userErrorVO = new UserOperationResultVO();
            userErrorVO.setMessage(e.getMessage());
            return new Response(4004, userErrorVO);
        }
        UserOperationResultVO userOperationResultVO = new UserOperationResultVO();
        userOperationResultVO.setMessage(String.valueOf(result));
        return new Response(1001, userOperationResultVO);
    }

    @RequestMapping("/user/delete")
    public Response userDelete(@RequestParam(value = "userId", required = false) Long userId,
                               @VerifiedUser User loginUser) {
        if (BaseUtils.isEmpty(loginUser)) {
            return new Response(1002);
        }
        int result;
        try {
            result = userService.delete(userId);
        } catch (RuntimeException e) {
            UserOperationResultVO userErrorVO = new UserOperationResultVO();
            userErrorVO.setMessage(e.getMessage());
            return new Response(4004, userErrorVO);
        }
        UserOperationResultVO userOperationResultVO = new UserOperationResultVO();
        userOperationResultVO.setMessage(result == 1 ? "success" : "fail");
        return new Response(1001, userOperationResultVO);
    }
}
